﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace converter.template
{
    /// <summary>
    /// An embedded image definition found in an .rdl file.
    /// </summary>
    public sealed class EmbeddedImage
    {
        /// <summary>
        /// Constructs a new instance of the class.
        /// </summary>
        /// <param name="name">Name of the image. Unique per report definition file.</param>
        /// <param name="mimeType">The MIME type. Expected values are: image/bmp, image/jpeg, image/gif, image/png, image/x-png</param>
        /// <param name="data">Base-64 encoded image data.</param>
        public EmbeddedImage(string name, string mimeType, string data)
        {
            this.name = name;
            this.mimeType = mimeType;

            byte[] buffer = Convert.FromBase64String(data);

            // Image dimensions.
            MemoryStream stream = new MemoryStream(buffer);
            using (System.Drawing.Image image = System.Drawing.Image.FromStream(stream))
            {
                width = image.Width;
                height = image.Height;
            }

            // Signed bytes are required by Windward.
            this.data = ByteToSByte(buffer);
        }

        /// <summary>
        /// Name of the image. Used as a primary key in a template's list of images.
        /// </summary>
        /// <returns></returns>
        public string GetName()
        {
            return name;
        }

        /// <summary>
        /// MIME type of the image. Expected values are: image/bmp, image/jpeg, image/gif, image/png, image/x-png
        /// </summary>
        /// <returns></returns>
        public string GetMimeType()
        {
            return mimeType;
        }

        /// <summary>
        /// File name is constructed of the image's name and an extention taken from the MIME type.
        /// </summary>
        /// <returns></returns>
        public string GetFileName()
        {
            int pos = mimeType.IndexOf('/');
            if (pos != -1)
                return string.Format("{0}.{1}", name, mimeType.Substring(pos + 1));
            else
                return "";
        }

        /// <summary>
        /// The image's bytes.
        /// </summary>
        /// <returns></returns>
        public sbyte[] GetData()
        {
            return data;
        }

        /// <summary>
        /// Width of the image.
        /// </summary>
        /// <returns></returns>
        public int GetWidth()
        {
            return width;
        }

        /// <summary>
        /// Height of the image.
        /// </summary>
        /// <returns></returns>
        public int GetHeight()
        {
            return height;
        }

        private static sbyte[] ByteToSByte(byte[] bytes)
        {
            sbyte[] sbytes = new sbyte[bytes.Length];
            for (int i = 0; i < bytes.Length; i++)
                sbytes[i] = (sbyte)bytes[i];
            return sbytes;
        }

        private readonly string name;
        private readonly string mimeType;
        private readonly int width;
        private readonly int height;
        private readonly sbyte[] data;
    }
}
